"""
@author: Sergiy Tsymbalyuk
"""

from sqlalchemy import Column, ForeignKey, Table
from sqlalchemy.types import Integer, String, Date, DateTime, Enum, Boolean, Numeric
from sqlalchemy.orm import relation

from legacy.model.meta import Base

__all__ = ['InterimDepartment', 'InterimAppointment', 'InterimUser', 'InterimDouble']


class Udk(Base):
    __tablename__ = 'm_kadry_udk'
    
    id = Column(Integer, primary_key=True)
    udk_name = Column(String(100))
    udk_name_new = Column(String(100))
    udk_address = Column(String(255))
    udk_telephone = Column(String(100))
    nach = Column(String(50))
    
    
class Department(Base):
    __tablename__ = 'm_kadry_department'
    
    id = Column(Integer, primary_key=True)
    depart_name = Column(String(100))


class PostType(Base):
    __tablename__ = 'm_kadry_post_type'
    
    id = Column(Integer, primary_key=True)
    type = Column(String(50))
    
    
class Post(Base):
    __tablename__ = 'm_kadry_post'
    
    id = Column(Integer, primary_key=True)
    post_name = Column(String(100))
    post_type = Column(Integer, ForeignKey(PostType.id))


class Personal(Base):
    __tablename__ = 'm_kadry_personal'
    
    id = Column(Integer, primary_key=True)
    pid = Column(Integer)
    last_name = Column(String(50))
    first_name = Column(String(50))
    second_name = Column(String(50))
    tuid = Column(Integer, ForeignKey(Udk.id))
    
    date_st_wk = Column(Date)
    nk_st_wk = Column(String(15))
    date_nk_st_wk = Column(Date)
    date_end_wk = Column(Date)
    nk_end_wk = Column(String(15))
    
    date_birth = Column(Date)
    sex = Column(Enum('M', 'F'))
    
    com_int_work = Column(Date)
    gov_work = Column(Date)
    finans_work = Column(Date)
    
    department_id = Column(Integer, ForeignKey(Department.id))
    post_id = Column(Integer, ForeignKey(Post.id))
    range = Column(Integer)
    
    def __unicode__(self):
        return '%s %s %s' % (self.last_name, self.first_name, self.second_name)
    

class PostChange(Base):
    __tablename__ = 'm_kadry_post_change'
    
    id = Column(Integer, primary_key=True)
    pid = Column(Integer, ForeignKey(Personal.id))
    id_depart = Column(Integer, ForeignKey(Department.id))
    id_post = Column(Integer, ForeignKey(Post.id))
    salary = Column(Numeric(precision=10, scale=2))
    date_assign = Column(Date)
    n_nak_assign = Column(String(50))
    date_nak = Column(Date)
    tuid = Column(Integer, ForeignKey(Udk.id))
    
    
class RangChange(Base):
    __tablename__ = 'm_kadry_rang_change'
    
    id = Column(Integer, primary_key=True)
    pid = Column(Integer, ForeignKey(Personal.id))
    rang = Column(Integer)
    date_rang = Column(Date)
    n_nak_rang = Column(String(15))
    date_nak_rang = Column(Date)
    
    
class Vacation(Base):
    __tablename__ = 'm_kadry_vacation'
    
    id = Column(Integer, primary_key=True)
    pid = Column(Integer, ForeignKey(Personal.id))
    date_vac = Column(Date)
    used = Column(Enum('Y', 'N'))
    date_used = Column(Date)
    n_nak_used = Column(String(15))
    date_nak_used = Column(Date)
    
    
class Vidpustka(Base):
    __tablename__ = 'm_kadry_vidpustka'
    
    id = Column(Integer, primary_key=True)
    id_pers = Column(Integer, ForeignKey(Personal.id))
    additional = Column(Integer)
    days = Column(Integer)
    period = Column(String(20))
    start = Column(Date)
    end = Column(Date)
    reason = Column(String(64))
    pay = Column(Integer)


class Kvalification(Base):
    __tablename__ = 'm_kadry_kvalification'
    
    id = Column(Integer, primary_key=True)
    id_pers = Column(Integer, ForeignKey(Personal.id))
    tuid = Column(Integer, ForeignKey(Udk.id))
    date_kval = Column(Date)
    institute = Column(String(50))
    kvalification = Column(String(50))


class Awards(Base):
    __tablename__ = 'm_kadry_awards'
    
    id = Column(Integer, primary_key=True)
    name = Column(String(100))
    
    
class WhoAwards(Base):
    __tablename__ = 'm_kadry_who_awards'
    
    id = Column(Integer, primary_key=True)
    name = Column(String(100))
    
    
class AwardsChange(Base):
    __tablename__ = 'm_kadry_awards_change'
    
    id = Column(Integer, primary_key=True)
    pid = Column(Integer, ForeignKey(Personal.id))
    id_depart = Column(Integer, ForeignKey(Department.id))
    id_post = Column(Integer, ForeignKey(Post.id))
    id_awards = Column(Integer, ForeignKey(Awards.id))
    id_who_awards = Column(Integer, ForeignKey(WhoAwards.id))
    n_nak_assign = Column(String(50))
    date_nak = Column(Date)
    tuid = Column(Integer, ForeignKey(Udk.id))
    
    
class Osvita(Base):
    __tablename__ = 'm_kadry_osvita'
    
    id = Column(Integer, primary_key=True)
    osvita_name = Column(String(75))
    

class OsvitaChange(Base):
    __tablename__ = 'm_kadry_osvita_change'
    
    id = Column(Integer, primary_key=True)
    pid = Column(Integer, ForeignKey(Personal.id))
    id_osvita = Column(Integer, ForeignKey(Osvita.id))
    osvita_spec = Column(String(75))
    institute = Column(String(100))
    date_osvita = Column(Date)
    dipl_number = Column(String(20))
    qualification = Column(String(100))


class Children(Base):
    __tablename__ = 'm_kadry_children'
    
    id = Column(Integer, primary_key=True)
    pid = Column(Integer, ForeignKey(Personal.id))
    name_child = Column(String(100))
    sex = Column(Enum('M', 'F'))
    date_birth = Column(Date)
    
    
class Workbook(Base):
    __tablename__ = 'm_kadry_workbook'
    
    idwb = Column(Integer, primary_key=True)
    pid = Column(Integer, ForeignKey(Personal.id))
    start = Column(Date)
    end = Column(Date)
    fin = Column(Boolean, default=False)
    derzh = Column(Boolean, default=False)
    kazna = Column(Boolean, default=False)
    work = Column(String(64))


class Staff(Base):
    __tablename__ = 'm_kadry_staff'
    
    id = Column(Integer, primary_key=True)
    rik = Column(Date)
    tuid = Column(Integer, ForeignKey(Udk.id))
    depart = Column(Integer, ForeignKey(Department.id))
    post = Column(Integer, ForeignKey(Post.id))
    oklad = Column(Integer)
    prim = Column(Integer)
    
    
class Users(Base):
    __tablename__ = 'm_kadry_users'
    
    id = Column(Integer, primary_key=True)
    user = Column(String(50))
    password = Column(String(35))
    name1 = Column(String(50))
    name2 = Column(String(50))
    name3 = Column(String(50))
    posada = Column(String(100))
    udk = Column(Integer, ForeignKey(Udk.id))
    tel = Column(String(50))
    date_create = Column(DateTime)
    disabled = Column(Boolean)


class InterimDepartment(Base):
    __tablename__ = 'i_kadry_department'
    
    id = Column(Integer, primary_key=True)
    district_id = Column(Integer)
    treasury_code = Column(Integer, ForeignKey(Udk.id))
    department_id = Column(Integer, ForeignKey(Department.id))


interim_positions = Table('i_kadry_position', Base.metadata,
    Column('post_id', Integer, ForeignKey(Post.id)),
    Column('position_id', Integer)
)


class InterimAppointment(Base):
    __tablename__ = 'i_kadry_appointment'
    
    id = Column(Integer, primary_key=True)
    
    position_id = Column(Integer)
    division_id = Column(Integer)
    employee_id = Column(Integer)
    
    number = Column(String(50))
    approved = Column(Date)
    executed = Column(Date)
    

class InterimUser(Base):
    __tablename__ = 'i_user'
    
    id = Column(Integer, primary_key=True)
    user_name = Column(String(16), unique=True)
    password = Column(String(80))
    employee_id = Column(Integer)
    
class InterimDouble(Base):
    __tablename__ = 'i_double'
    
    id = Column(Integer, primary_key=True)
    person_id = Column(Integer)
    